import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
import Home from '@/pages/Home'
import Search from '@/pages/Search'
import Login from '@/pages/Login'
import Register from '@/pages/Register'

//先把vueRouter原型对象的push保存一份
let orginPush = VueRouter.prototype.push;
let orginReplace = VueRouter.prototype.replace;
//重写push|replace
//第一个参数：告诉原来push方法 往哪里跳
VueRouter.prototype.push =function(loaction, resolve, reject) {
    if (resolve && reject) {
        //call和apply区别
        //相同点：都可以调用函数一次 都可以篡改函数的上下文一次
        //不同点：call和apply传递参数：call传递参数用逗号隔开，apply方法执行传递数组
        orginPush.call(this,loaction,resolve, reject);
    } else {
        orginPush.call(this,loaction,()=>{},()=>{})
    }
}
VueRouter.prototype.replace =function(loaction, resolve, reject) {
    if (resolve && reject) {
        //call和apply区别
        //相同点：都可以调用函数一次 都可以篡改函数的上下文一次
        //不同点：call和apply传递参数：call传递参数用逗号隔开，apply方法执行传递数组
        orginReplace.call(this,loaction,resolve, reject);
    } else {
        orginReplace.call(this,loaction,()=>{},()=>{})
    }
}
export default new VueRouter({
    routes:[
        {
            path: '/home',
            component: Home,
            meta:{show: true}
        },
        {
            path: '/search/:keyword',
            component: Search,
            meta:{show: true},
            name: 'search'
        },
        {
            path: '/login',
            component: Login,
            meta:{show: false}
        },
        {
            path: '/register',
            component: Register,
            meta:{show: false}
        },
        // 重定向 项目运行时访问/ 直接定向
        {
            path: '*',
            redirect: "/home"
        }
    ]
})